package com.sxm.yiwei.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.VendorNoServiceActivity;
import com.sxm.yiwei.beans.ServiceList;
import com.sxm.yiwei.tools.CastTime;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.x;

import java.util.List;

/**
 * Created by KuKa on 2017/7/13.
 *  服务 卖家列表
 */

public class VendorBookListAdapter extends BaseAdapter {
    Context context;
   public List<ServiceList.DataBean> mlist ;
    OnClickItemButtonListener o;
    int TYPE;
    public VendorBookListAdapter(Context context,List<ServiceList.DataBean> mlist,int TYPE) {
        this.context=context;
        this.mlist = mlist;
        this.TYPE = TYPE;
    }

    @Override
    public int getCount() {
        return mlist.size()==0?0:mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView==null){
            view = View.inflate(context, R.layout.vendor_listiem,null);
            holder = new ViewHolder();
            holder.buyername = (TextView) view.findViewById(R.id.vendor_buyer_name);
            holder.buyeraddress = (TextView) view.findViewById(R.id.vendor_buyer_address);
            holder.vendorname = (TextView) view.findViewById(R.id.vendor_vendor_name);
            holder.vendoraddress = (TextView) view.findViewById(R.id.vendor_vendor_address);
            holder.addpricebt = (TextView) view.findViewById(R.id.vendor_item_addpricebt);
            holder.callbt = (LinearLayout) view.findViewById(R.id.vendor_item_cellbt);
            holder.cancelbt = (TextView) view.findViewById(R.id.vendor_item_cancelbt);
            holder.weiyiabt = (LinearLayout) view.findViewById(R.id.vendor_item_weiyiabt);
            holder.beginbt = (TextView) view.findViewById(R.id.vendor_item_beginservice);

            holder.name = (TextView) view.findViewById(R.id.vendor_serviceexplain);
            holder.price = (TextView) view.findViewById(R.id.vendor_serviceexplain1);
            holder.message = (TextView) view.findViewById(R.id.vendor_serviceexplaintext);
            holder.bookno = (TextView) view.findViewById(R.id.vendor_servicebooknotext);
            holder.allprice = (TextView) view.findViewById(R.id.vendor_servicepricetext);
            holder.addno = (TextView) view.findViewById(R.id.vendor_serviceaddprice);
            holder.number = (TextView) view.findViewById(R.id.vendor_service_num);
            holder.time = (TextView) view.findViewById(R.id.vendor_servicetime);
            holder.img = (ImageView) view.findViewById(R.id.vendor_serviceimg);
            holder.view1 = (LinearLayout) view.findViewById(R.id.vendor_listiem_view);
            holder.money = (TextView) view.findViewById(R.id.vendor_listaddmoney);
            holder.itemview = (RelativeLayout) view.findViewById(R.id.vendor_item_view);
            view.setTag(holder);
        }else {
            view=convertView;
            holder = (ViewHolder) view.getTag();
        }

        if (TYPE==4&TYPE==5){

            holder.view1.setVisibility(View.GONE);
        }else {


            int isMakeup = mlist.get(position).getIsMakeup();

            if (isMakeup==1){

                holder.view1.setAlpha(0.2f);
                holder.money.setText(mlist.get(position).getMakeupAmount()+" 元");
                holder.view1.setVisibility(View.VISIBLE);
            }else {

                holder.view1.setVisibility(View.GONE);
            }
        }
        if (TYPE==1){
            holder.cancelbt.setText("取消");
            holder.beginbt.setText("开始服务");
        }else if (TYPE==2){
            holder.cancelbt.setText("申诉");
            holder.beginbt.setText("服务完结");
        }else if (TYPE==3){
            holder.cancelbt.setText("申诉");
            holder.beginbt.setVisibility(View.GONE);
        }else if (TYPE==4){
            holder.cancelbt.setText("申诉");
            holder.addpricebt.setVisibility(View.GONE);
            holder.beginbt.setVisibility(View.GONE);
        }else {
            holder.cancelbt.setText("查看");
            holder.addpricebt.setVisibility(View.GONE);
            holder.beginbt.setVisibility(View.GONE);
        }
        holder.buyername.setText("买家:"+mlist.get(position).getContactName()+" "+mlist.get(position).getContactPhone());
        holder.buyeraddress.setText(mlist.get(position).getContactAddress()+""+mlist.get(position).getContactHouseNumber());
        holder.vendorname.setText("卖家:"+mlist.get(position).getUserMobile());
        holder.vendoraddress.setText(mlist.get(position).getShopAddress());
        holder.money.setText(mlist.get(position).getMakeupAmount()+" 元");
        holder.name.setText(mlist.get(position).getName());
        holder.time.setText(CastTime.CastTime(mlist.get(position).getBuyTime()));
        holder.number.setText("数量: "+mlist.get(position).getCount());
        holder.price.setText("单价 "+mlist.get(position).getAmount()+"元/单");
        holder.allprice.setText(mlist.get(position).getTotalAmount()+" 元");
        if (mlist.get(position).getMakeupCount()==0){
            holder.addno.setText("加价 0 次");
        }else {
            holder.addno.setText("加价 "+mlist.get(position).getMakeupCount()+"次");
        }
        holder.bookno.setText(mlist.get(position).getOrderCode());
        holder.message.setText(mlist.get(position).getMessage());
        x.image().bind(holder.img, InternetAddressUtis.IMG_URL+mlist.get(position).getAvatar());

        holder.weiyiabt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener(position);
                }
            }
        });
        holder.callbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener1(position);
                }
            }
        });
        holder.cancelbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener2(position);
                }
            }
        });
        holder.addpricebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener3(position);
                }
            }
        });
        holder.beginbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener4(position);
                }
            }
        });
        holder.view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener5(position);
                }
            }
        });
        holder.itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener6(position);
                }
            }
        });
        notifyDataSetChanged();
        return view;
    }

    class ViewHolder{
        LinearLayout callbt,weiyiabt,view1;
        RelativeLayout itemview;
        TextView addpricebt,beginbt,cancelbt,money,buyername,buyeraddress,vendorname,vendoraddress;
        TextView name,price,allprice,time,number,bookno,addno,message;
        ImageView img;
    }
    public interface OnClickItemButtonListener{
        void OnClickItemButtonListener(int position);
        void OnClickItemButtonListener1(int position);
        void OnClickItemButtonListener2(int position);
        void OnClickItemButtonListener3(int position);
        void OnClickItemButtonListener4(int position);
        void OnClickItemButtonListener5(int position);
        void OnClickItemButtonListener6(int position);
    }
    public void setOnClickItemButtonListener(OnClickItemButtonListener o){
        this.o=o;
    }
}
